package com.its.commons.redisDao.impl;

import com.its.commons.redisDao.RedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisDaoImpl<T> implements RedisDao<T> {
    //依赖注入RedisTemplate
    @Autowired
    private RedisTemplate<String, T> redisTemplate;

    @Override
    public void set(String key, T value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void set(String key, T value, int expire, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, expire, timeUnit);
    }

    @Override
    public boolean setIfAbsent(String key, T value) {
        return redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    @Override
    public boolean setIfAbsent(String key, T value, int expire, TimeUnit timeUnit) {
        return redisTemplate.opsForValue().setIfAbsent(key, value, expire, timeUnit);
    }

    @Override
    public T get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean expire(String key, int expire, TimeUnit timeUnit) {
        return redisTemplate.expire(key, expire, timeUnit);
    }

    @Override
    public Long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }

    @Override
    public boolean del(String key) {
        return redisTemplate.delete(key);
    }

    @Override
    public boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }
    //缓存数据到Redis中的List结构
    @Override
    public void leftPushAll(String key, Collection<T> values) {
        redisTemplate.opsForList().leftPushAll(key, values);
    }
    //获取Redis中的List结构中的数据
    @Override
    public List<T> range(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end);
    }
}
